package com.sq.es.templete;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author Li sir
 * @Date 2024/1/8
 * es 和 index 相关 操作
 */
@Component
public class ElasticSearchIndexTemplete {

    @Autowired
    private ElasticSearchConnectionTemplete elasticSearchConnectionTemplete;

    public CreateIndexResponse createIndex(String indexName){
        RestHighLevelClient elasticClient = elasticSearchConnectionTemplete.getConnection();
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            CreateIndexResponse createIndexResponse = elasticClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            return createIndexResponse;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            elasticSearchConnectionTemplete.replaceClient(elasticClient);
        }
    }

    public void createIndex(String indexName,String a){
        RestHighLevelClient elasticClient = elasticSearchConnectionTemplete.getConnection();
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
//            createIndexRequest.mapping()
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            elasticSearchConnectionTemplete.replaceClient(elasticClient);
        }
    }

    public GetIndexResponse getIndex(String indexName){
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
        RestHighLevelClient elasticClient = elasticSearchConnectionTemplete.getConnection();
        try {
            GetIndexResponse getIndexResponse = elasticClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
            System.out.println("getIndexResponse = " + getIndexResponse);
            return getIndexResponse;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            elasticSearchConnectionTemplete.replaceClient(elasticClient);
        }
    }

    public AcknowledgedResponse deleteIndex(String indexName){
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        RestHighLevelClient elasticClient = elasticSearchConnectionTemplete.getConnection();
        try {
            AcknowledgedResponse delete = elasticClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
            return delete;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            elasticSearchConnectionTemplete.replaceClient(elasticClient);
        }
    }

}
